package cn.school.demo.repository

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import cn.school.demo.pojo.Comment
import cn.school.demo.pojo.Result
import cn.school.demo.util.HttpApi
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.google.gson.reflect.TypeToken
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class CommentRepository {
    private val httpApi: HttpApi

    init {
        val retrofit =
            Retrofit.Builder().baseUrl(DataRepository.BASEURL).addConverterFactory(
                GsonConverterFactory.create()
            )
                .build()
        httpApi = retrofit.create(HttpApi::class.java)
    }

    fun getUserCommentById(page: Int, size: Int, articleId: Int): LiveData<MutableList<Comment>> {
        val responseLiveData = MutableLiveData<MutableList<Comment>>()
        httpApi.getUserCommentById(mapOf("page" to page, "size" to size, "articleId" to articleId))
            .enqueue(object : Callback<Result> {
                override fun onResponse(call: Call<Result>, response: Response<Result>) {
                    response.body()?.let {
                        if (it.success) {
                            it.data["comment"]?.let { jsonData ->
                                try {
                                    val map = GsonUtils.fromJson<MutableList<Comment>>(
                                        jsonData.toString(),
                                        object : TypeToken<MutableList<Comment>>() {}.type
                                    )
                                    LogUtils.d(map.toString())
                                    map?.let {
                                        responseLiveData.value = it
                                    } ?: LogUtils.e("map为空！！")
                                } catch (e: Error) {
                                    e.printStackTrace()
                                }
                            } ?: LogUtils.e("用户评论http返回值的data[\"comment\"]为空！")
                        } else {
                            LogUtils.e(it.message)
                            responseLiveData.value = mutableListOf()
                        }
                    } ?: LogUtils.e("用户评论http返回值为空！")
                }

                override fun onFailure(call: Call<Result>, t: Throwable) {
                    LogUtils.e(t)
                }
            })
        return responseLiveData
    }

    fun saveUserCommentById(
        sfNumber: Int,
        commentContent: String,
        articleId: Int
    ): LiveData<Result> {
        val responseLiveData = MutableLiveData<Result>()
        httpApi.saveUserCommentById(
            mapOf(
                "sfNumber" to sfNumber,
                "commentContent" to commentContent,
                "articleId" to articleId
            )
        ).enqueue(object : Callback<Result> {
            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                response.body()?.let {
                    responseLiveData.value = it
                } ?: LogUtils.e("保存评论http返回值为空！")
            }

            override fun onFailure(call: Call<Result>, t: Throwable) {
                LogUtils.e(t)
            }
        })
        return responseLiveData
    }
}